package com.example.Words.room.ui.Adapter;

/**
 * <pre>
 *  author:huletian
 *  time    :2020/2/22
 *  desc    :显示列表的适配器
 *  version:1.0
 * </pre>
 */

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.example.Words.R;
import com.example.Words.room.model.entity.Word;
import com.example.Words.room.presenter.RoomPresenterIntf;

import java.util.ArrayList;
import java.util.List;


public class RoomViewAdapter extends ListAdapter<Word,RoomViewAdapter.MyViewHolder> {
    boolean useCardView;
    RoomPresenterIntf roomPresenterIntf;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public  TextView numbertv, englishtv, chinesetv;
        Switch chineseInvisible;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            numbertv = itemView.findViewById(R.id.number);
            englishtv = itemView.findViewById(R.id.english);
            chinesetv = itemView.findViewById(R.id.chinese);
            chineseInvisible=itemView.findViewById(R.id.chineseInvisible);
        }
    }

    public RoomViewAdapter(boolean useCardView, RoomPresenterIntf roomPresenterIntf) {
        super(new DiffUtil.ItemCallback<Word>() {
            @Override
            public boolean areItemsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return oldItem.getId()==newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull Word oldItem, @NonNull Word newItem) {
                return (oldItem.getWord().equals(newItem.getWord())
                        &&oldItem.getChineseMeaning().equals(newItem.getChineseMeaning())
                        &&oldItem.isChineseInvisible()==newItem.isChineseInvisible()
                        );
            }
        });
        this.useCardView = useCardView;
        this.roomPresenterIntf=roomPresenterIntf;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;
        if (useCardView) {
            itemView = layoutInflater.inflate(R.layout.cell_card_2, parent, false);
        } else itemView = layoutInflater.inflate(R.layout.cell_normal_2, parent, false);
        MyViewHolder myViewHolder=new MyViewHolder(itemView);
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("http://www.youdao.com/w/" + myViewHolder.englishtv.getText() + "/#keyfrom=dict2.top");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                myViewHolder.itemView.getContext().startActivity(intent);
            }
        });
        myViewHolder.chineseInvisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Word word=(Word) myViewHolder.itemView.getTag(R.id.word_for_view_holdet);
                if(isChecked){
                    myViewHolder.chinesetv.setVisibility(View.GONE);
                    word.setChineseInvisible(true);
                    roomPresenterIntf.update(word);
                }else {
                    myViewHolder.chinesetv.setVisibility(View.VISIBLE);
                    word.setChineseInvisible(false);
                    roomPresenterIntf.update(word);
                }
            }
        });
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        final Word word = getItem(position);
        holder.itemView.setTag(R.id.word_for_view_holdet,word);
        holder.numbertv.setText(String.valueOf(position + 1));
        holder.englishtv.setText(word.getWord());
        holder.chinesetv.setText(word.getChineseMeaning());
        if (word.isChineseInvisible()){
            holder.chinesetv.setVisibility(View.GONE);
            holder.chineseInvisible.setChecked(true);
        }else{
            holder.chinesetv.setVisibility(View.VISIBLE);
            holder.chineseInvisible.setChecked(false);
        }

    }

    @Override
    public void onViewAttachedToWindow(@NonNull MyViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.numbertv.setText(String.valueOf(holder.getAdapterPosition()+1));
    }
}
